<template>
  <div>
    <el-card style="margin: 20px 0px">
      <CategorySelect
        @getCategoryId="getCategoryId"
        :show="scene!=0"
      ></CategorySelect>
    </el-card>
    <el-card>
      <!-- 底部这里是三个部分切换 -->
      <div v-show="scene == 0">
        <!-- 展示spu列表结结构 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addSpu"
          :disabled="!category3Id"
          >添加Spu</el-button
        >
        <el-table style="width: 100%" border :data="records">
          <el-table-column
            type="index"
            label="序号"
            align="center"
            width="80"
          ></el-table-column>
          <el-table-column
            prop="spuName"
            label="spu名称"
            width="width"
          ></el-table-column>
          <el-table-column
            prop="description"
            label="spu描述"
            width="width"
          ></el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <!-- 这里的按钮使用hintButtom替换 -->
              <el-button
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="uppdateSpu(row)"
              ></el-button>
              <el-button
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看当前spu全部sku列表"
                @click="handler(row)"
              ></el-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="deleteSpu(row)">
                <el-button type="danger" icon="el-icon-delete" size="mini" title="删除spu" slot="reference"></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!--  -->
        <el-pagination
          style="margin-top: 20px; text-align: center"
          :current-page="page"
          :total="total"
          :page-size="limit"
          :pager-count="5"
          :page-sizes="[3, 5, 10]"
          @current-change="getSpuList"
          @size-change="handleSizeChange"
          layout="prev,pager,next,jumper,->,sizes,total"
        >
        </el-pagination>
      </div>
      <SpuForm v-show="scene == 1" @changeScene="changeScene" ref="spu" ></SpuForm>
      <SkuForm v-show="scene == 2" @changeScenes="changeScenes" ref="sku"></SkuForm>
      <!--  -->
    </el-card>
    <el-dialog :title="`${spu.spuName}的Sku列表`" :visible.sync="dialogTableVisible" :before-close="close">
      <el-table :data="skuList" style="width:100%" border v-loading="loading">
        <el-table-column prop="skuName" label="名称" width=""></el-table-column>
        <el-table-column prop="price" label="价格" width=""></el-table-column>
        <el-table-column prop="weight" label="重量"></el-table-column>
        <el-table-column label="默认图片">
          <template slot-scope="{row,$index}">
            <img :src="row.skuDefaultImg" style="height:100px;weight:100px"></img>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
// 引入子组件
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";

export default {
  name: "Spu",
  data() {
    return {
      // 分别是分类的id
      category1Id: "",
      category2Id: "",
      category3Id: "",
      // 控制三级联动的可操作性
      isShowTable: true,
      page: 1, // 当前第几页
      limit: 5, // 每页需要展示多少条数据
      records: [], // spu列表的数据
      total: 0, // 分页器一共需要展示的数据条数
      scene: 0, // 0 代表战术Spu列表的数据    1 添加Spu|Spu   2 添加Sku
      dialogTableVisible:false, // 控制列表对话框的显示与隐藏
      spu:{},
      skuList:[],
      loading:true,
    };
  },
  methods: {
    // 三级联动的自定义事件，可以吧子组件的id传递给父组件
    getCategoryId({ categoryId, level }) {
      // categoryId 获取到一二三分类的id level：为了区分是几级id
      if (level == 1) {
        this.category1Id = categoryId;
        // 清空category23Id
        this.category2Id = "";
        this.category3Id = "";
      } else if (level == 2) {
        this.category2Id = categoryId;
        // 清空category3Id
        this.category3Id = "";
      } else {
        this.category3Id = categoryId;
        // 获取spu列表进行展示
        this.getSpuList();
      }
    },
    // 获取spu列表数据的方法
    async getSpuList(pages = 1) {
      // 点击分页器第几页按钮的回调
      this.page = pages;
      const { page, limit, category3Id } = this;
      // 携带三个参数
      let result = await this.$API.spu.reqSpuList(page, limit, category3Id);
      console.log(result);
      if (result.code == 200) {
        this.total = result.data.total;
        this.records = result.data.records;
      }
    },
    // 添加spu按钮的回调
    addSpu() {
      // 切换场景为1
      this.scene = 1;
      // 通知子组件SpuForm发起请求-----两个
      this.$refs.spu.addSpuData(this.category3Id);
    },
    // 修改某一个spu
    uppdateSpu(row) {
      this.scene = 1;
      // 获取子组件SpuForm子组件
      // 在父组件当中通过$ref获取子组件
      this.$refs.spu.initSpuData(row);
    },
    // 当分页器的某一页发生变化的回调
    handleSizeChange(limit) {
      // 修改参数,再次发请求
      this.limit = limit;
      this.getSpuList();
    },
    // spuForm 自定义事件的回调
    changeScene({scene,flag}) {
      // flag是为了区分保存与修改
      // 切换结构（场景）
      this.scene = scene;
      // 子组件通知父组件切换场景，需要在次获取spu列表的数据进行展示
      if (flag=="修改"){
        this.getSpuList(this.page);
      }else{
        this.getSpuList();
      }
    },
    // skuForm 自定义事件的回调   子组件通知父组件切换场景
    changeScenes(scene){
      this.scene = scene;
    },
    // 删除spu的回调
    async deleteSpu(row){
      let result = await this.$API.spu.reqDeleteSpu(row.id);
      if (result.code==200){
        this.$message({type:"success",message:"删除成功"});
        // this.records.length>1?this.page:this.page-1
        // 代表spu列表数据个数大于1时，停留在当前页面，小于1时，停留在上一页
        this.getSpuList(this.records.length>1?this.page:this.page-1);
      }
    },
    // 添加sku的回调
    addSku(row){
      // 切换场景2
      this.scene = 2;
      // 让父组件调用子组件的方法------让子组件发起请求
      this.$refs.sku.getData(this.category1Id,this.category2Id,row);

    },
    // 查看sku按钮的回调
    async handler(spu){
      this.dialogTableVisible = true;
      this.spu = spu;
      // 获取sku列表的数据进行展示
      let result = await this.$API.spu.reqSkuList(spu.id);
      if (result.code==200){
        this.skuList = result.data;
        this.loading = false;  // 隐藏loading属性
      }
    },
    close(done){
      // loading属性再次变为
      this.loading = true;
      // 清除数据
      this.skuList = [];
      // 关闭对话框
      done();
    }
  },
  components: {
    SpuForm,
    SkuForm,
  },
};
</script>

<style>
</style>